package com.he.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Notice;
import com.he.pojo.User;
import com.he.pojo.WorkflowStepDetail;
import com.he.service.notice.NoticeServiceI;

@Controller
@RequestMapping("notice")
public class NoticeController {

	@Autowired
	private NoticeServiceI noticeServiceI;

	/**
	 * @author jiao
	 * @date 2018年8月23日 显示所有公告
	 */
	@RequestMapping("showAllNotice")
	public String showAllNotice(ModelMap modelMap, String flag,Notice notice) {
		List<Notice> list = noticeServiceI.getNotices(null, notice);
		modelMap.addAttribute("noticeList", list);
		/*为notice总页数赋值,向前端传总页数*/
		PageInfo<Notice> info=new PageInfo<>(list);
		//为notice总页数赋值
		notice.setPages(info.getPages());
		//为notice总记录数赋值
		notice.setTotal(info.getTotal());
		/*为notice返回的controller链接赋值*/
		notice.setUrl("showAllNotice.do");
		/*向前端传参数保存在workflow中*/
		modelMap.addAttribute("notice", notice);
		return "notice";
	}

	/**
	 * 显示公告详细信息
	 * 
	 * @author jiao
	 * @date 2018年8月23日
	 */
	@RequestMapping("showDetail")
	public String showDetail(ModelMap modelMap, int id) {
		Notice notice = noticeServiceI.getNoticeById(id);
		int readNum = notice.getReadNum();
		notice.setReadNum(readNum + 1);
		noticeServiceI.update(notice);
		modelMap.addAttribute("notice", notice);
		return "notice_detail";
	}

	/**
	 * 转发页面,直接跳转不可以实现 创建新的公告
	 * 
	 * @author jiao
	 * @date 2018年8月23日
	 */
	@RequestMapping("creatNotice")
	public String creatNotice() {
		return "new_notice";
	}

	/**
	 * 
	 * 实现删除公告 删除后跳转回显示界面
	 * 
	 * @author jiao
	 * @date 2018年8月24日
	 */
	@RequestMapping("deleteNotice")
	public String deleteNotice(ModelMap modelMap, int id) {
		int result = noticeServiceI.delete(id);
		if (result > 0) {
			return "redirect:showAllNotice.do";
		} else {
			return "redirect:showAllNotice.do";
		}
	}

	/**
	 * 修改公告 根据id
	 * 
	 * @author jiao
	 * @date 2018年8月24日
	 */
	@RequestMapping("editNotice")
	public String editNotice(ModelMap modelMap, int id) {
		Notice notice = noticeServiceI.getNoticeById(id);
		modelMap.addAttribute("notice", notice);
		modelMap.addAttribute("noticeid", id);
		return "edit_notice";
	}

	/**
	 * 保存修改后的公告
	 * 
	 * @author jiao
	 * @date 2018年8月24日
	 */
	@RequestMapping("saveEdit")
	@ResponseBody
	public String saveEdit(HttpServletRequest request) {
		String noticeInfo = request.getParameter("noticeInfo");
		String noticeTitle = request.getParameter("noticeTitle");
		String isPublish = request.getParameter("isPublish");
		Integer noticeId=Integer.valueOf(request.getParameter("noticeId"));
		Notice notice = new Notice();
		notice.setId(noticeId);
		notice.setNoticeInfo(noticeInfo);
		notice.setNoticeTitle(noticeTitle);
		if (isPublish.equals("已发布")) {
			notice.setIsPublish((byte) 1);
		} else if (isPublish.equals("待发布")) {
			notice.setIsPublish((byte) 0);
		}
		int r = noticeServiceI.update(notice);
		System.out.println(r);
		if (r > 0) {
			return "success";
		} else {
			return "false";
		}
	}

	/*
	 * @Description: 保存新创建的公告 接收ajax传过来的数值
	 * @author jiao
	 * @date 2018年8月24日
	 */

	@RequestMapping("saveNotice")
	@ResponseBody
	public String saveNotice(HttpServletRequest request) {
		String noticeInfo = request.getParameter("noticeInfo");
		String noticeTitle = request.getParameter("noticeTitle");
		String isPublish = request.getParameter("isPublish");
		Notice notice = new Notice();
		System.out.println(isPublish);
		notice.setNoticeInfo(noticeInfo);
		notice.setNoticeTitle(noticeTitle);
		User user=(User) request.getSession().getAttribute("user");
		notice.setUserId(user.getId());
		if (isPublish.equals("已发布")) {
			notice.setIsPublish((byte) 1);
		} else if (isPublish.equals("待发布")) {
			notice.setIsPublish((byte) 0);
		}
		notice.setReadNum(0);
		int r = noticeServiceI.insert(notice);
		if (r > 0) {
			return "success";
		} else {
			return "false";
		}
	}
}
